Grokking Microservices Design Patterns
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
The Course at a Glance
Strangler Fig Pattern
Introduction
The Problem: Legacy Systems
The Strangler Pattern: A Solution
The Architecture of the Strangler Pattern
Strangler Pattern: A Detailed Example
Key Insights and Implications
API Gateway Pattern
Introduction to the API Gateway Pattern
Advantages of API Gateway Pattern
API Gateway Pattern: An Example
Performance Implications
System Design Example
Backends for Frontends (BFF) Pattern
Introduction to BFF
The Problem: Traditional Backend Models
The Architecture of the BFF Pattern
BFF Pattern: An Example
Performance Implications
System Design Examples
Service Discovery Pattern
What is Service Discovery Pattern?
The Problem: Service Coordination in Distributed Systems
Service Discovery Pattern: A Solution
The Architecture of the Service Discovery Pattern
The Inner Workings of the Service Discovery Pattern
Service Discovery Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Security Considerations
Circuit Breaker Pattern
Introduction
The Problem: The Struggles of Distributed Systems and Service Failures
The Circuit Breaker Pattern: An Effective Shield Against Cascading Failures
Circuit Breaker Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Summary
Bulkhead Pattern
Introduction
The Problem: Failure Propagation in Distributed Systems
The Bulkhead Pattern: A Solution
The Architecture
The Inner Workings
Bulkhead Pattern: A Example
Performance Implications and Special Considerations
System Design Examples
Conclusion
Retry Pattern
Introduction
The Retry Pattern: A Solution to Unreliable External Resources
The Architecture of the Retry Pattern
Retry Pattern: An Example
Performance Implications
Use Cases and System Design Examples
Conclusion
Sidecar Pattern
Introduction to the Sidecar Pattern
The Problem: Monolithic Application Management
A Solution to the Monolithic Mayhem
The Architecture of the Sidecar Pattern
Sidecar Pattern: Bringing Theory to Practice with an Example
Performance Implications
System Design Examples: Bringing the Sidecar Pattern to Life
Saga Pattern
Introduction to Saga Pattern
The Problem: Traditional Transaction Models
The Saga Pattern: A Solution
The Architecture of the Saga Pattern
The Inner Workings of the Saga Pattern
Saga Pattern: A Example
Performance Implications
System Design Examples
Conclusion
Introduction
The Problem: Managing Complex Interactions in Distributed Systems
Event-Driven Architecture: A Promising Solution
The Architecture of the Event-Driven Architecture Pattern
The Inner Workings of the Event-Driven Architecture Pattern
Event-Driven Architecture Pattern: An Example
Performance Implications and Special Considerations
Use Cases and System Design Examples
Conclusion
Introduction
The Problem: Traditional CRUD Operations
CQRS Pattern: A Solution
The Architecture of the CQRS Pattern
The Inner Workings of the CQRS Pattern
CQRS Pattern: An Example
Issues, Special Considerations, and Performance Implications
System Design Examples
Introduction
The Problem: Configuration Management in a Microservices Architecture
The Solution: Configuration Externalization Pattern
Unveiling the Architecture: How Does Configuration Externalization Work?
Delving into Code: An Example
Considerations and Implications
Use Cases and Real-world Examples
Conclusion
Embrace the Future of Software Architecture
The Saga Pattern: A Solution

As we've seen, the challenge is real, and the need for a solution is urgent. Enter the Saga Pattern, a design pattern that has proven to be a highly effective way to manage transactions in the complex landscape of distributed systems.

A New Hope: Introduction to the Saga Pattern

In the simplest terms, a saga is a sequence of local transactions. Each local transaction updates the database and publishes an event to trigger the next local transaction in the saga. If a local transaction fails because it violates a business rule, the saga executes a series of compensating transactions that undo the changes that were made by the preceding local transactions.

Can you imagine a team of skilled surgeons performing a complicated operation? Each surgeon has a specific task to perform, but they're all part of a larger coordinated effort. That's a useful metaphor for understanding the Saga pattern. Each service in a distributed system performs its local transaction, but all these transactions are coordinated to achieve the larger goal of maintaining data consistency across services.

The Saga Pattern brings a different approach to maintaining data consistency in a distributed system, one that is more suited to the challenges posed by distributed systems. Instead of trying to enforce strict consistency as ACID does, Saga acknowledges the fact that distributed transactions will span across multiple services and multiple databases. So, it opts for a more practical eventual consistency model.

Unraveling the Magic: How the Saga Pattern Works

Now that we've introduced the Saga pattern, let's delve deeper into how it works. Each transaction in a saga is paired with a compensating transaction that can undo the changes made by the transaction. When a saga is successfully completed, all its transactions have been executed. However, if a transaction fails during the execution, the saga orchestrates the execution of the compensating transactions for the transactions that have been already executed. This ensures that the system remains in a consistent state.

The saga maintains the integrity of the system by ensuring that either all transactions are completed, or compensation transactions are run to roll back the ones that were previously executed.

This makes sagas ideal for long-lived, high-level business transactions that span multiple microservices. They can ensure data consistency across globally distributed and highly available database systems.

However, designing sagas can be complex because they require intricate coordination between participating services and extensive error handling.

Does it sound too complicated? Let's break it down even further.

The Two Types of Saga Patterns

There are two types of Saga Patterns: Choreography and Orchestration.

Choreography

In the Choreography Saga Pattern, every local transaction publishes domain events that trigger local transactions in other services. There is no central coordination. Instead, each service produces and listens to other service's events and decides if an action should be taken or not.

Imagine an orchestra, where no conductor is present. Each musician listens to the other instruments and plays their part in harmony. That's the essence of the Choreography Saga Pattern.

Orchestration

The Orchestration Saga Pattern introduces a central coordinator (the saga orchestrator) that is responsible for executing the steps of a saga. It tells the participants when to execute their local transactions.

Going back to our orchestra, the Orchestration Saga Pattern includes a conductor (the saga orchestrator) who guides all musicians (the local transactions) to create a symphony (the business transaction).

Now, you might be wondering, "How do I choose between choreography and orchestration?" A good rule of thumb is to consider the complexity of your business transaction. If it is simple, choreography could be a good option as it requires less coordination. However, as the transaction becomes more complex, orchestration can provide better visibility and control over the business transaction.

Unveiling the Hero: A Real-World Analogy

If we imagine our distributed system as a city, each service can be seen as a building. The traditional ACID transactions are like the internal wiring of each building, ensuring that within the building, electricity flows to where it's needed.

But what happens when you want to connect two buildings? You can't just extend the internal wiring – it wouldn't be safe or efficient. You need a different solution, like a power line. That's what the Saga Pattern does. It's the power line connecting our buildings, allowing them to work together, while also ensuring safety and consistency.

A Peep into the Future: The Saga Pattern and Microservices

With the growing adoption of microservices architecture, the Saga pattern is gaining popularity. This pattern provides a viable solution to manage transactions and ensure data consistency in a microservice architecture, where each microservice has its own database.

By now, you should have a good understanding of the problems posed by distributed transactions, and how the Saga pattern provides a solution. But theory is only the start - let's see how this would work in practice, shall we?

The Problem: Traditional Transaction Models
The Architecture of the Saga Pattern
Mark as Completed

On this page

A New Hope: Introduction to the Saga Pattern

Unraveling the Magic: How the Saga Pattern Works

The Two Types of Saga Patterns

Choreography

Orchestration

Unveiling the Hero: A Real-World Analogy

A Peep into the Future: The Saga Pattern and Microservices